﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationGenerator.Lists.BusinessLogic;

namespace GUserInterface.WinForms.Lists
{
    public partial class ColumnFilteredConfiguration : Form
    {
        private FilterColumn _selectedFilter;
        private IList<Column> _availableColumns;
        private IList<ColumnFilterType> _availableFiltersTypes;

        public FilterColumn SelectedFilter
        {
            get
            {
                if (_selectedFilter == null)
                {
                    _selectedFilter = new FilterColumn();
                }
                return _selectedFilter;
            }
            set { _selectedFilter = value; }
        }

        public ColumnFilteredConfiguration(IList<Column> availableColumns)
        {
            InitializeComponent();
            InitializeFiltersType();
            _availableColumns = availableColumns;
        }

        private void InitializeFiltersType()
        {
            _availableFiltersTypes = new List<ColumnFilterType>();
            //_availableFiltersTypes.Add(ColumnFilterType.Boolean);
            //_availableFiltersTypes.Add(ColumnFilterType.Number);
            _availableFiltersTypes.Add(ColumnFilterType.Text);
        }

        public void InitializeColumns()
        {
            if (_availableColumns.Count == 0)
            {
                MessageBox.Show("Brak wybranych kolumn");
                DialogResult = DialogResult.Abort;
                this.Close();
            }

            cbColumns.DataSource = _availableColumns;
            cbColumns.DisplayMember = "Name";

            cbFilters.DataSource = _availableFiltersTypes;

            cbColumns.SelectedIndex = 0;
            cbFilters.SelectedIndex = 0;

            if (SelectedFilter.RelatedColumn != null)
            {
                cbColumns.SelectedItem = _selectedFilter.RelatedColumn;
                cbFilters.SelectedItem = _selectedFilter.FilterType;
                tbFilterName.Text = _selectedFilter.DisplayName;
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tbFilterName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Należy podać wyświetlaną nazwę");
                return;
            }

            SelectedFilter.DisplayName = tbFilterName.Text;
            SelectedFilter.RelatedColumn = cbColumns.SelectedItem as Column;
            SelectedFilter.FilterType = (ColumnFilterType)cbFilters.SelectedItem;

            DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
